package com.example.osixaffairssystem.controller;

import com.example.osixaffairssystem.domain.PageBean;
import com.example.osixaffairssystem.domain.Result;
import com.example.osixaffairssystem.domain.model.Score;
import com.example.osixaffairssystem.service.ScoreService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/score")
@RequiredArgsConstructor
public class ScoreController {

    final ScoreService scoreService;

    @GetMapping("/list")
    public Result list() {
        log.info("获取成绩列表");
        List<Score> list = scoreService.list();
        return Result.success(list);
    }

    @GetMapping
    public Result listToPage(@RequestParam(name = "page") Integer page,
                             @RequestParam(name = "pageSize") Integer pageSize) {
        log.info("分页查询课程{ page: {}, pageSize: {} }", page, pageSize);
        PageBean<Score> list = scoreService.listToPage(page, pageSize);
        return Result.success(list);
    }

    @PostMapping
    public Result insert(@RequestBody Score score) {
        log.info("新增成绩: {}", score);
        Integer id = scoreService.insert(score);
        return Result.success(id);
    }

    @PutMapping
    public Result update(@RequestBody Score score) {
        log.info("修改成绩: {}", score);
        scoreService.update(score);
        return Result.success();
    }

    @DeleteMapping("/{ids}")
    public Result delete(@PathVariable List<Integer> ids) {
        log.info("删除成绩: {}", ids);
        scoreService.delete(ids);
        return Result.success();
    }
}
